package org.t2.game.server.module.mail.service;

import java.util.Collections;
import java.util.List;

import org.gaming.prefab.mail.IMailTemplateDesign;
import org.gaming.prefab.mail.MailArgs;
import org.gaming.prefab.mail.MailBelong;
import org.gaming.prefab.mail.MailPoolAbstractService;
import org.gaming.service.repository.IRepository;
import org.gaming.service.repository.Repository;
import org.gaming.tool.LocalDateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.t2.game.server.foundation.player.PlayerContext;
import org.t2.game.server.foundation.player.PlayerContextManager;
import org.t2.game.server.module.mail.message.SystemMailMessage;
import org.t2.game.server.module.mail.model.Mail;
import org.t2.game.server.module.mail.model.MailPlayer;
import org.t2.game.server.module.mail.model.MailSystem;
import org.t2.game.server.module.player.model.Player;
import org.t2.game.server.module.player.service.PlayerService;

/**
 * @author yh
 * @date 2023/6/1
 * @apiNote
 */
@Service
public class MailSystemService extends MailPoolAbstractService<MailSystem, Mail> {

    @Autowired
    private MailPlayerService mailPlayerService;
    @Autowired
    private PlayerContextManager playerContextManager;
    @Autowired
    private MailConverter mailConverter;
    @Autowired
    private PlayerService playerService;
    
    private IRepository<MailSystem> repository;
    protected IRepository<MailSystem> repository() {
        if (repository == null) {
            repository = Repository.get(MailSystem.class);
        }
        return repository;
    }

    @Override
    public void saveMail(MailSystem mail) {
        this.repository().insert(mail);
        
        if(mail.getBelong() == MailBelong.EVERYONE) {
        	for(PlayerContext playerContext : playerContextManager.onlines()) {
        		SystemMailMessage message = SystemMailMessage.of(playerContext.getId());
        		playerContext.tell(message);
        	}
        } else {
        	for(long playerId : mail.getReceivers()) {
        		PlayerContext playerContext = playerContextManager.getContext(playerId);
        		if(playerContext != null) {
        			SystemMailMessage message = SystemMailMessage.of(playerContext.getId());
            		playerContext.tell(message);
        		}
        	}
        }
    }

    @Override
    protected Mail createMail(long receiver) {
        return new Mail();
    }

    @Override
    protected MailSystem createMailPool() {
        return new MailSystem();
    }

    @Override
    protected List<MailSystem> allInPool() {
        return repository().getAll();
    }

    public MailSystem getMailSystem(long id) {
        return repository().getByKeys(id);
    }

    public void delete(MailSystem mailSystem) {
        repository().delete(mailSystem);
    }

    @Override
    protected IMailTemplateDesign getTemplate(int template) {
    	IMailTemplateDesign design = new IMailTemplateDesign() {
			@Override
			public int getTemplateId() {
				return 0;
			}

			@Override
			public List<String> getTitleFormatRules() {
				return Collections.emptyList();
			}

			@Override
			public List<String> getContentFormatRules() {
				return Collections.emptyList();
			}

			@Override
			public int getExpireDay() {
				return 30;
			}
    	};
        return design;
    }

    @Override
    protected List<String> convertArgs(List<String> list, MailArgs mailArgs) {
        return mailConverter.changerArgs(list, mailArgs.getArgs());
    }

    public List<Mail> pullMailFromSystem(long playerId) {
        MailPlayer mailPlayer = mailPlayerService.getEntity(playerId);
        Mails mails = this.checkUnreadMails(mailPlayer.getPlayerId(), mailPlayer.getLastReadTime());
        if (mails.getMails().isEmpty()) {
            return Collections.emptyList();
        }
        mailPlayer.setLastReadTime(mails.getLastReadTime());
        mailPlayer.setReadTime(LocalDateTimeUtil.ofEpochSecond(mails.getLastReadTime()));
        mailPlayerService.update(mailPlayer);
        return mails.getMails();
    }

	@Override
	protected boolean customCondition(long receiver, MailSystem mailPool) {
		Player player = playerService.getEntity(receiver);
		int bornTime = LocalDateTimeUtil.toEpochSecond(player.getInsertTime());
		if(bornTime > mailPool.getPostTime()) {
			return false;
		}
		return true;
	}
}
